import { loginByUsername, logout, getUserInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { Message } from 'element-ui'

const user = {
  state: {
    user: '',
    status: '',
    code: '',
    token: getToken(),
    name: '',
    avatar: '',
    introduction: '',
    roles: [],
    setting: {
      articlePlatform: []
    }
  },

  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_USERID: (state, userId) => {
      state.userId = userId
    },
    SET_HOSPITALID: (state, hospitalId) => {
      state.hospitalId = hospitalId
    }
  },

  actions: {
    // 用户登录
    LoginByUsername({ commit }, userInfo) {
      // console.log(userInfo)
      return new Promise((resolve, reject) => {
        loginByUsername(userInfo).then(response => {
          const data = response.data
          // console.log(data)
          if (data.code == 200) {
            commit('SET_TOKEN', data.data.token)
            commit('SET_NAME', data.data.phone)
            sessionStorage.setItem('token', data.data.token)
            sessionStorage.setItem('check_status', data.data.check_status)
            sessionStorage.setItem('update_status', data.data.update_status)
            sessionStorage.setItem('userName', data.data.phone)
            sessionStorage.setItem('source', data.data.source)
            sessionStorage.setItem('card_status', data.data.card_status)
            sessionStorage.setItem('card_tag', data.data.card_tag)
            // sessionStorage.setItem('check_msg', data.data.check_msg)
            setToken(data.data.token)
            resolve(true)
          } else {
            Message.error(data.msg)
          }
        }).catch(error => {
          // console.log(error)
          Message.error('接口访问失败')
          reject(error)
        })
      })
    },

    // 获取用户信息
    // GetUserInfo({ commit, state }) {
    //   // commit('SET_NAME', "666")
    //   return new Promise((resolve, reject) => {
    //     getUserInfo(state.token).then(response => {
    //       // sessionStorage.setItem("username",response.data.data.username)
    //       // console.log(response)
    //       //   const data = response.data.data
    //       //   if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
    //       //     commit('SET_ROLES',  data.roles)
    //       //   } else {
    //       //     reject('权限配置失败！禁止登陆！')
    //       //   }
    //       if (!response.data.code) {
    //         // commit('SET_NAME', response.data.data)
    //         commit('SET_NAME', response.data.data.phone)
    //         commit('SET_USERID', response.data.data.userId)
    //         // commit('SET_HOSPITALID', response.data.data.hospitalId)
    //         // console.log(response.data.data)
    //         if (response.data.data.username == 'admin') {
    //           const permissionJson = ['adminRole']
    //           sessionStorage.setItem('permissionJson', JSON.stringify(permissionJson))
    //         }
    //       }

    //       //   commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')
    //       //   commit('SET_INTRODUCTION', '美美咖')
    //       resolve(response)
    //     })
    //       .catch(error => {
    //         reject(error)
    //       })
    //   })
    // },

    // 第三方验证登录
    // LoginByThirdparty({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    LogOut({ commit, state }) {
      // return new Promise((resolve, reject) => {
      //   logout(state.token).then(() => {
      sessionStorage.clear()
      // console.log(111111111)
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      // resolve()
      //   }).catch(error => {
      //     reject(error)
      //   })
      // })
    }

    // 前端 登出
    // FedLogOut({ commit }) {
    //   return new Promise(resolve => {
    //     commit('SET_TOKEN', '')
    //     sessionStorage.clear()
    //     removeToken()
    //     resolve()
    //   })
    // },

    // 动态修改权限
    // ChangeRoles({ commit, dispatch }, role) {
    //   return new Promise(resolve => {
    //     commit('SET_TOKEN', role)
    //     setToken(role)
    //     getUserInfo(role).then(response => {
    //       const data = response.data
    //       commit('SET_ROLES', data.roles)
    //       commit('SET_NAME', data.name)
    //       commit('SET_AVATAR', data.avatar)
    //       commit('SET_INTRODUCTION', data.introduction)
    //       dispatch('GenerateRoutes', data) // 动态修改权限后 重绘侧边菜单
    //       resolve()
    //     })
    //   })
    // }
  }
}

export default user
